//
//  DHHeaderViewCell.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/11.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHHeaderViewCell.h"

@interface DHHeaderViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *hotelImageView; // 酒店照片
@property (weak, nonatomic) IBOutlet UILabel *hotalTitle; //title
@property (weak, nonatomic) IBOutlet UILabel *adressLabel; // 详细地址 加 距离
@property (weak, nonatomic) IBOutlet UILabel *clearTypeLabel; //清洁类型 label
@property (weak, nonatomic) IBOutlet UILabel *serviceTimeLabel; // 服务时间
@property (weak, nonatomic) IBOutlet UILabel *contactNum; // 联系电话
@property (weak, nonatomic) IBOutlet UILabel *contactName; // 联系人
@property (weak, nonatomic) IBOutlet UILabel *priceLabel; // 预计价钱
@property (weak, nonatomic) IBOutlet UILabel *endTimeLabel; // 截止日期 FIXME: - 未实现倒数
@property (weak, nonatomic) IBOutlet UILabel *payValueLabel; //
@property (weak, nonatomic) IBOutlet UILabel *taskLabel;
@property (weak, nonatomic) IBOutlet UILabel *payUnitLabel;
@property (weak, nonatomic) IBOutlet UILabel *payCountLabel;
@property (weak, nonatomic) IBOutlet UIImageView *evaIconView; //   评论人头像
@property (weak, nonatomic) IBOutlet UILabel *evaNameLabel; // 评价人名称
@property (weak, nonatomic) IBOutlet UIView *evaStarView;   // 评价星星
@property (weak, nonatomic) IBOutlet UILabel *evaContentLabel;
@property (weak, nonatomic) IBOutlet UIView *evaPicView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *evaPicViewHeightCons;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *evaIconHeightCons;
@property(nonatomic ,strong)HYBStarEvaluationView *starView;
@end
@implementation DHHeaderViewCell



//FIXME: - 星星有问题


- (void)setModel:(DHDetailModel *)model{
    _model = model;
    
    if (!model) {return;};
    self.hotalTitle.text = model.title;
    [self.hotelImageView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    //self.hotalTitle.text = model
    self.adressLabel.text = [NSString stringWithFormat:@"%.02f km %@",model.distance.doubleValue / 1000,model.address_info];
    self.clearTypeLabel.text = model.clear_type;
    self.serviceTimeLabel.text = model.arrive_time;
    self.contactNum.text = model.contcat_mobile;
    self.contactName.text = model.contcat_name;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@",model.price];
    self.endTimeLabel.text = model.end_time;
    self.payValueLabel.text = model.pay_value;
    self.payUnitLabel.text = model.pay_unit;
    self.payCountLabel.text = model.pay_count;
    if (model.evaluate.count > 0) {
        [self.evaIconView sd_setImageWithURL:[NSURL URLWithString:model.evaluate.firstObject.from_avatar]];
        self.evaNameLabel.text = model.evaluate.firstObject.from_name;
        
        [model.evaluate.firstObject.imageList
         enumerateObjectsUsingBlock:
         ^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
         {
             UIImageView *iv = self.evaPicView.subviews[idx];
             iv.hidden = false;
             
             [iv sd_setImageWithURL:[NSURL URLWithString:obj]];
         }];
        
        //self.starView.starCount = model.evaluate.firstObject.scores.integerValue;
        self.starView.actualScore = model.evaluate.firstObject.scores.floatValue;
        self.evaContentLabel.text = model.evaluate.firstObject.content;
        return;
    }
    self.evaPicViewHeightCons.constant = 0;
    self.evaIconHeightCons.constant = 0;

    self.evaContentLabel.hidden = true;
    self.evaNameLabel.hidden = true;
    self.evaStarView.hidden = true;
    [self.contentView layoutIfNeeded];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self setupEvaPicView];
    self.starView = [[HYBStarEvaluationView alloc]initWithFrame:self.evaStarView.bounds numberOfStars:5 isVariable:YES];
    
    self.starView.isContrainsHalfStar = YES;//是否包含半颗星，默认为NO
    self.starView.fullScore = 5;
    [self.evaStarView addSubview:self.starView];
    self.starView.userInteractionEnabled = false;


}

#pragma mark: - 创建图片
- (void)setupEvaPicView{
    
    //计算评论照片尺寸
    CGFloat margin = 3;
    CGFloat singleImageW = (self.evaPicView.bounds.size.width - 4 * margin) / 6;
    
    //遍历创建评论照片
    for (int i = 0 ; i < 5; i++) {
        
        UIImageView *iv = [[UIImageView alloc] init];
        iv.hidden = true;
        iv.frame = CGRectMake(i * (singleImageW + margin),
                              0,
                              singleImageW,
                              self.evaPicView.bounds.size.height);
        //iv.backgroundColor = UIColor.yellowColor;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPhoto:)];
        [iv addGestureRecognizer:tap];
        iv.tag = i;
        iv.userInteractionEnabled = true;
        [self.evaPicView addSubview:iv];
    }
}

#pragma mark: - 评价图像点击监听
- (void)showPhoto:(UITapGestureRecognizer *)tap
{
    if (self.model.evaluate.firstObject.imageList.count == 0) {return;}
    UIImageView *imgView = (UIImageView *)tap.view;
    //点击的图像索引
    NSInteger idx = imgView.tag;
    
    NSMutableArray<UIImageView *> *ivList = [NSMutableArray array];
    for (UIImageView *iv in self.evaPicView.subviews)
    {
        if (!iv.isHidden) {[ivList addObject:iv];}
    }
    
    //获取图像url数组
    NSArray *imgUrlList = self.model.evaluate.firstObject.imageList;
    
    [NSNotificationCenter.defaultCenter postNotificationName:kCommentPhotoBrowerNotification
                                                      object:nil
                                                    userInfo:@{@"imgViewIdx":@(idx),
                                                               @"imgUrl":imgUrlList,
                                                               @"ivList":ivList
                                                               }];
    
}
- (IBAction)checkEvaButtonClick
{
    if (!self.model.evaluate) {
        [SVProgressHUD showErrorWithStatus:@"没有更多评论"];
        return;
        
    }
    [NSNotificationCenter.defaultCenter postNotificationName:kNotificationPushController object:nil userInfo:@{@"model":self.model}];
}

@end
